package com.example.mall.common.utils;

import com.example.mall.common.core.response.BaseResponse;
import com.example.mall.common.core.response.SuccessResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public class SuccessHelper {
    private static final String KEY = "data";

    /**
     * 包装响应结果
     *  - 默认 key: data
     *  - 格式:
     *      {
     *          code: 0,
     *          message: "ok",
     *          data: {
     *              [data]: [null]
     *          }
     *      }
     * @param key
     * @param o
     * @return
     */
    public static SuccessResponse excute(String key,Object o) {
        if ("".equals(key)) {
            key = KEY;
        }
        Map<String, Object> result = new HashMap<>();
        result.put(key, o);
        return new SuccessResponse(result);
    }

    public static SuccessResponse excute(Object o) {
        return SuccessHelper.excute(KEY, o);
    }

    public static SuccessResponse excute() {
        return SuccessHelper.excute(KEY, null);
    }
}
